package com.derbysoft.synxisadapter.dswitch2.translator.impl;

import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.generic.constants.UniqueIDType;
import com.derbysoft.remote.dswitch.hotel.dto.CancelRQ;
import com.derbysoft.synxisadapter.commons.utils.Constant;
import com.derbysoft.synxisadapter.commons.dto.synxis.BasicPropertyInfoType;
import com.derbysoft.synxisadapter.commons.dto.synxis.OTACancelRQ;
import com.derbysoft.synxisadapter.commons.dto.synxis.TPAExtensions;
import com.derbysoft.synxisadapter.commons.dto.synxis.Verification;
import com.derbysoft.synxisadapter.commons.exception.ReservationNumberNotFoundException;
import com.derbysoft.synxisadapter.commons.model.Reservation;
import com.derbysoft.synxisadapter.commons.model.ReservationItem;
import com.derbysoft.synxisadapter.commons.repository.ReservationRepository;
import com.derbysoft.synxisadapter.dswitch2.translator.Translator;

import java.util.ArrayList;
import java.util.List;

public class CancelRQTranslatorImpl extends AbstractRQTranslator implements Translator<GenericRQ<CancelRQ>, List<OTACancelRQ>, Void> {
    private ReservationRepository reservationRepository;

    public void setReservationRepository(ReservationRepository reservationRepository) {
        this.reservationRepository = reservationRepository;
    }

    @Override
    public List<OTACancelRQ> translate(GenericRQ<CancelRQ> rq, Void param) {

        ArrayList<OTACancelRQ> otaCancelRQs = new ArrayList<OTACancelRQ>();

        String ersp = TranslateHelper.find(
                rq.getPayload().getHotelReservationIDs(), UniqueIDType.ERSP);
        Reservation reservation = reservationRepository.findByErspCode(ersp);
        if (reservation == null) {
            throw new ReservationNumberNotFoundException(ersp);
        }

        for (ReservationItem reservationItem : reservation.getReservationItems()) {
            OTACancelRQ otaCancelRQ = createOTACancelRQ();
            otaCancelRQ.setPOS(translatePOS(rq.getHeader().getSource()));
            otaCancelRQ.setPrimaryLangID(Constant.PRIMARY_LANG_ID);

            otaCancelRQ.getUniqueID().add(
                    translateUniqueID(UniqueIDType.RESERVATION.getCode(),
                            reservationItem.getConfirmNumber(), Constant.CONTEXT_CRS_CONFIRM_NUMBER));
            otaCancelRQ.setVerification(translateVerification(reservation));
            otaCancelRQs.add(otaCancelRQ);
        }

        return otaCancelRQs;
    }


    private Verification translateVerification(Reservation reservation) {
        Verification verification = createVerification();
        verification.setTPAExtensions(translateTPAExtensions(reservation.getHotelId()));
        return verification;
    }

    private TPAExtensions translateTPAExtensions(String hotelCode) {
        TPAExtensions extensions = createTPAExtensions();
        extensions.setBasicPropertyInfo(translateBasicPropertyInfoType(hotelCode));
        return extensions;
    }

    private BasicPropertyInfoType translateBasicPropertyInfoType(String hotelCode) {
        BasicPropertyInfoType basicPropertyInfoType = createBasicPropertyInfoType();
        basicPropertyInfoType.setHotelCode(hotelCode);
        return basicPropertyInfoType;
    }
}